package hot_100;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.concurrent.atomic.AtomicInteger;

public class 和为K的子数组 {
    public static void main(String[] args) {

    }

    public int subarraySum(int[] nums, int k) {
        int n = nums.length;
        long[] sum = new long[n + 1];
        for (int i = 0; i < n; i++) {
            sum[i + 1] = sum[i] + nums[i];
        }
        HashMap<Long, Integer> map = new HashMap<>();
        int ans = 0;
        map.put(0L, 1);
        for (int i = 1; i <= n; i++) {
            ans += map.getOrDefault(sum[i] - k, 0);
            map.merge(sum[i], 1, Integer::sum);
        }
        return ans;
    }
}
